//
//  Theme.swift
//  CooperSwift
//
//  Created by Mac on 2019/12/19.
//  Copyright © 2019 CooperSwift. All rights reserved.
//

import Foundation
import UIKit

let theme = ThemeManager.shared.theme

//MARK: - narbar
struct NavBarTheme {
    // 背景色
    var backgroundColor: UIColor
    // 标题
    var titleColor: UIColor
    var titleFont: UIFont
    // 按钮
    var barButtonTitleColor: UIColor
    var barButtonTitleFont: UIFont
    var barButtonDisabledTitleColor: UIColor
    //
    var backImage: UIImage
}

//MARK: - tabbar
struct TabBarTheme {
    // 背景色
    var backgroundColor: UIColor
    // normal
    var titleColor: UIColor
    var titleFont: UIFont
    // selected
    var selectedTitleColor: UIColor
    var selectedTitleFont: UIFont
    //
    var bgImage: UIImage
}

//MARK: - 协议
protocol ThemeProtocol {
    var mainColor: UIColor { get }
    var backgroundColor: UIColor { get }
    
    var titleColor: UIColor { get }
    var titleFont: UIFont { get }
    
    var textFont: UIFont { get }
    var textColor: UIColor { get }
    
    var detailColor: UIColor { get }
    var detailFont: UIFont { get }
    
    var navbarTheme: NavBarTheme { get }
    var tabbarTheme: TabBarTheme { get }
}

// 默认值
//extension ThemeProtocol {
//    var backgroundColor: UIColor { return UIColor(hex: "#f2f3f4") }
//
//    var titleFont: UIFont { return UIFont.systemFont(ofSize: 17) }
//    var titleColor: UIColor { return UIColor(hex: "#ffffff") }
//
//    var textFont: UIFont { return UIFont.systemFont(ofSize: 17) }
//    var textColor: UIColor { return UIColor(hex: "#333333") }
//
//    var detailFont: UIFont { return UIFont.systemFont(ofSize: 15) }
//    var detailColor: UIColor { return UIColor(hex: "#666666") }
//
//    var navbarTheme: NavBarTheme { return NavBarTheme(backgroundColor: self.mainColor, titleColor: self.titleColor, titleFont: self.titleFont, barButtonTitleColor: self.titleColor, barButtonTitleFont: self.detailFont, barButtonDisabledTitleColor: self.titleColor.alpha(0.5), backImage: UIImage(named: "img_black")!.withRenderingMode(.alwaysOriginal)) }
//
//    var tabbarTheme: TabBarTheme { return TabBarTheme(backgroundColor: UIColor(hex: "#ffffff"), titleColor: self.detailColor, titleFont: UIFont.systemFont(ofSize: 13), selectedTitleColor: self.mainColor, selectedTitleFont: UIFont.systemFont(ofSize: 13), bgImage: UIImage()) }
//
//}

//MARK: - 主题
struct BlueTheme: ThemeProtocol {
    
    var mainColor: UIColor { return UIColor(hex: "#57B3F9") }
    
    var backgroundColor: UIColor { return UIColor(hex: "#57B3F9") }
    
    var titleColor: UIColor { return UIColor(hex: "#ffffff") }
    
    var titleFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var textColor: UIColor { return UIColor(hex: "#333333") }
    
    var textFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var detailColor: UIColor { return UIColor(hex: "#666666") }
    
    var detailFont: UIFont { return UIFont.systemFont(ofSize: 15) }
    
    var navbarTheme: NavBarTheme { return NavBarTheme(backgroundColor: self.mainColor, titleColor: self.titleColor, titleFont: self.titleFont, barButtonTitleColor: self.titleColor, barButtonTitleFont: self.detailFont, barButtonDisabledTitleColor: self.titleColor.alpha(0.5), backImage: UIImage(named: "img_black")!.withRenderingMode(.alwaysOriginal)) }
    
    var tabbarTheme: TabBarTheme { return TabBarTheme(backgroundColor: UIColor(hex: "#ffffff"), titleColor: self.detailColor, titleFont: UIFont.systemFont(ofSize: 13), selectedTitleColor: self.mainColor, selectedTitleFont: UIFont.systemFont(ofSize: 13), bgImage: UIImage()) }
    
}

struct WhiteTheme: ThemeProtocol {
    
    var mainColor: UIColor { return UIColor(hex: "#ffffff") }
    
    var backgroundColor: UIColor { return UIColor(hex: "#57B3F9") }
    
    var titleColor: UIColor { return UIColor(hex: "#ffffff") }
    
    var titleFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var textColor: UIColor { return UIColor(hex: "#333333") }
    
    var textFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var detailColor: UIColor { return UIColor(hex: "#666666") }
    
    var detailFont: UIFont { return UIFont.systemFont(ofSize: 15) }
    
    var navbarTheme: NavBarTheme { return NavBarTheme(backgroundColor: self.mainColor, titleColor: self.titleColor, titleFont: self.titleFont, barButtonTitleColor: self.titleColor, barButtonTitleFont: self.detailFont, barButtonDisabledTitleColor: self.titleColor.alpha(0.5), backImage: UIImage(named: "img_black")!.withRenderingMode(.alwaysOriginal)) }
    
    var tabbarTheme: TabBarTheme { return TabBarTheme(backgroundColor: UIColor(hex: "#ffffff"), titleColor: self.detailColor, titleFont: UIFont.systemFont(ofSize: 13), selectedTitleColor: self.mainColor, selectedTitleFont: UIFont.systemFont(ofSize: 13), bgImage: UIImage()) }
    
}

struct BlackTheme: ThemeProtocol {
    
    var mainColor: UIColor { return .init(hex: "#333333") }
    
    var backgroundColor: UIColor { return UIColor(hex: "#57B3F9") }
    
    var titleColor: UIColor { return UIColor(hex: "#ffffff") }
    
    var titleFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var textColor: UIColor { return UIColor(hex: "#333333") }
    
    var textFont: UIFont { return UIFont.systemFont(ofSize: 17) }
    
    var detailColor: UIColor { return UIColor(hex: "#666666") }
    
    var detailFont: UIFont { return UIFont.systemFont(ofSize: 15) }
    
    var navbarTheme: NavBarTheme { return NavBarTheme(backgroundColor: self.mainColor, titleColor: self.titleColor, titleFont: self.titleFont, barButtonTitleColor: self.titleColor, barButtonTitleFont: self.detailFont, barButtonDisabledTitleColor: self.titleColor.alpha(0.5), backImage: UIImage(named: "img_black")!.withRenderingMode(.alwaysOriginal)) }
    
    var tabbarTheme: TabBarTheme { return TabBarTheme(backgroundColor: UIColor(hex: "#ffffff"), titleColor: self.detailColor, titleFont: UIFont.systemFont(ofSize: 13), selectedTitleColor: self.mainColor, selectedTitleFont: UIFont.systemFont(ofSize: 13), bgImage: UIImage()) }
}


// 简单工厂
// MARK: - 主题枚举
enum ThemeType {
    case blueTheme
    case whiteTheme
    case blackTheme
    
    var theme: ThemeProtocol {
        switch self {
        case .blueTheme:
            return BlueTheme()
        case .whiteTheme:
            return WhiteTheme()
        case .blackTheme:
            return BlackTheme()
        }
    }
}

// MARK: - 主题管理
class ThemeManager {
    // 默认
    var theme:ThemeProtocol = BlueTheme()
    
    // 单利
    static let shared = ThemeManager()
    
    // 切换主题
    static func switchTheme(type: ThemeType) {
        ThemeManager.shared.switchTheme(type: type)
    }
    
    // 实现
    private func switchTheme(type: ThemeType) {
        self.theme = type.theme
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: Constants.ThemeNotificationChangeNotify), object: self.theme)
    }
    
}
